public class ListNode {
    int val; // 值域
    ListNode next; // 节点域
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution2 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        int length = getLength(head);
        ListNode cur = dummy;
        for (int i = 1; i < length - n + 1; ++i) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        ListNode ans = dummy.next;
        return ans;
    }

    public int getLength(ListNode head) {
        int length = 0;
        while (head != null) {
            ++length;
            head = head.next;
        }
        return length;
    }
}

class MySolution2 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode tail = head;
        ListNode dummy = new ListNode(0, head);
        ListNode cursor = dummy;
        int count = 0;
        while (tail.next != null) {
            tail = tail.next;
            count++;
        }
        count += 2;
        for (int i = 2; i < count - n + 1; i++) {
            cursor = cursor.next;
        }
        cursor.next = cursor.next.next;
        return dummy.next;
    }
}

class Solution1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // 创建新头尾节点，模拟一个链表
        ListNode head = null, tail = null;

        //运载参数，承载去尾后得到的数
        int carry = 0;

        while (l1 != null || l2 != null) {
            // 取素材节点（链表）中头节点的值
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;

            int sum = n1 + n2 + carry;

            // 更新结果节点（链表）
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }

            // 更新运载参数，保留十位上的数
            carry = sum / 10;

            // 更新素材节点（链表），指向第二个节点
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }

        // carry 不为 0，即十位上还有数，需再开一个节点存储
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }
}
class MySolution1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null;
        ListNode tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        if (carry != 0) {
            tail.next = new ListNode(carry);
            tail = tail.next;
        }
        return head;
    }
}
